public class demo {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        int result = binarySearch(nums, 3);
        System.out.println(result);
    }

    private static int binarySearch(int [] nums, int t) {
        int l = 0, r = nums.length - 1;
        while (l <= r){
            int mid = (l + r) >> 1;
            if(nums[mid] == t){
                return mid;
            }else if(nums[mid] < t){
                l = mid + 1;
            }else {
                r = mid - 1;
            }
        }
        return -1;
    }
}
